<?php

class Student_Model_DbTable_Tracker extends Zend_Db_Table_Abstract {

	protected $_name = 'assessment';
        public $currentAvg;
        
        //protected $_dbAlias = "";



       public function currentGrades($course_id)
       {
       $course = new Student_Model_DbTable_Course();
       $assess = new Student_Model_DbTable_Assessment();
       $grades = new Student_Model_DbTable_Grade();

       //$course_id = $this->_getParam('id');

       $allAssess = $assess->getSingleUserAssessments($course_id);
       //$name_array = Array();
       $grade_array = Array();
       foreach($allAssess as $assessment)
       {
           $assess_id = $assessment->id;
           $assess_name = "<u>".$assessment->assess_name."</u>:";
           $get_grade = $grades->getSingleUserGrades($assess_id);
           array_push($grade_array, $assess_name);
           foreach($get_grade as $g)
           {
                $grade = "<b>".$g->grade."</b>";

                //echo $grade;
                array_push($grade_array, $grade);
           }
           //array_push($name_array, $grade_array);
       }
       //echo $grade_array;
       return $grade_array; // this isn't right

    }

    public function currentGoal($course_id)
    {

       $course = new Student_Model_DbTable_Course();
       $assess = new Student_Model_DbTable_Assessment();
       $grades = new Student_Model_DbTable_Grade();
       $scale = new Student_Model_DbTable_Scale();

       //$course_id = $this->_getParam('id');

       $desired_grade = $course->getCourseDetails($course_id);
       $goal_grade;
       $goal_grade_output;
       foreach($desired_grade as $goal)
           {
                $goal_grade = $goal->desired_grade;
                $goal_grade_output = "Your desired grade for <b>$goal->course_name ($goal->course_number)</b> is '<b>$goal->desired_grade</b>'.<br>";


           }

      $minimum = $scale->getCourseScale($course_id);
      $min_grade=null;
      foreach($minimum as $min)
          {

          if($min->grade == $goal_grade)
            {
            
               //this goes to the views
              if(!$min->minimum == null){
               $min_grade = "To achieve '<b>$goal_grade</b>', your average needs to be atleast <b>$min->minimum%</b> in the course. <br> It is suggested that you achieve a grade of atleast <b>$min->minimum%</b> on every assessment within each group."
                       ."<br> Doing this puts equal amount of pressure on all your assessments.";
              }
          }
          }

      $goals = array();
      array_push($goals, $goal_grade_output, $min_grade);

      return $goals;

    }

    public function currentStanding($course_id)
    {

       $course = new Student_Model_DbTable_Course();
       $assess = new Student_Model_DbTable_Assessment();
       $grades = new Student_Model_DbTable_Grade();
       $scale = new Student_Model_DbTable_Scale();

       //$course_id = $this->_getParam('id');

       $allAssess = $assess->getSingleUserAssessments($course_id);

        $currentStanding = Array();
        $average_adder = 0;
        $average_counter = 0;
        $progress_output;

        $exclusion = "<u>Note</u>: <i>Assessment groups whose grades are not available will not be listed.</i><br>";
        array_push($currentStanding, $exclusion);
        foreach($allAssess as $assessment)
        {

          $assess_id = $assessment->id;
          
          $get_grade = $grades->getSingleUserGrades($assess_id);

          
          $num_of_assessments = $assess->getAssessDetails($assess_id);
          $number=0;
          $counter=0;
          $total=0;
          $average=0;
          $excluded=0;
          $num_of_assess_counter=0;
          $grade=0;
          $left_over = 0;
          
       
            foreach($num_of_assessments as $num)
            {
                //for($i=0; $i <= $num)
                $number = $num->num_of_assign;
                //echo "Total number of $assessment->assess_name: $number <br><br>";
                
            }

           
            foreach ($get_grade as $g)
          {
            //dummy/decoit
            $grade = $g->grade;
            $total = $total + $grade;

            if($grade != null)
            {
            $counter = $counter + 1;
            }
            else
               {
                $counter;
                echo $counter;
               }
            
          $left_over = $number - $counter;
         // echo $left_over;
          
          
          $average = round(($total/$counter), 2);
          }
          $assess_name_output = "<u>$num->assess_name:</u><br> ";
          $average_output = "Current Average: <b>$average%</b>"."<br>";
          $number_output = "Total assessments: <b>$number"."</b><br>";
          $left_over_output = "Remaining assessments: <b>$left_over</b> <br><br>";

          

          $average_adder = $average_adder + $average;
          //echo $average_adder."<br>";
          if ($average !=0){
          $average_counter = $average_counter + 1;
          //echo $average_counter."<br>";
          array_push($currentStanding, $assess_name_output,$average_output, $number_output, $left_over_output); 
          }
          else{
              $average_counter = $average_counter;
          }            

       $counter = 0;
       $average = 0;
       $total = 0;
       $left_over=0;
       }

       global $class_average;
       $class_avg_output = null;
       if(!$average_counter == 0){
       $class_average = round($average_adder/$average_counter, 2);
       }
       if (!$class_average == 0){
       $class_avg_output = "<b>Current class average: $class_average% </b><br>";
       }
       //assigning the average to the global variable

       $course_details = $course->getCourseDetails($course_id);
       $goal_grade;
       $num_of_assessments;
       
       foreach($course_details as $goal)
           {
                $goal_grade = $goal->desired_grade;
                
           }

      $minimum = $scale->getCourseScale($course_id);
      $min_grade;
      foreach($minimum as $min)
          {

          if($min->grade == $goal_grade)
            {              
               $min_grade = $min->minimum;
            }
          }     

       $num_of_groups = $assess->getSingleUserAssessments($course_id);
       //assessment groups counter
       $num_of_assess_counter=0;
       foreach($num_of_groups as $num){
           $num_of_assess_counter++ ;
       }

       

       //this is where all the data is shoved, later displayed in views
       array_push($currentStanding, $class_avg_output);
       return $currentStanding;

      }


      /* Dummy Function for the GPA calculator */
      public function currentAverage($course_id)
    {

       $course = new Student_Model_DbTable_Course();
       $assess = new Student_Model_DbTable_Assessment();
       $grades = new Student_Model_DbTable_Grade();
       $scale = new Student_Model_DbTable_Scale();

       //$course_id = $this->_getParam('id');

       $allAssess = $assess->getSingleUserAssessments($course_id);

        $currentStanding = Array();
        $average_adder = 0;
        $average_counter = 0;
        $progress_output=0;

        $exclusion = "<u>Note: Assessment groups whose grades are not available will not be listed.</u><br>";
        array_push($currentStanding, $exclusion);
        foreach($allAssess as $assessment)
        {

          $assess_id = $assessment->id;

          $get_grade = $grades->getSingleUserGrades($assess_id);


          $num_of_assessments = $assess->getAssessDetails($assess_id);
          $number;
          $counter=0;
          $total=0;
          $average=0;
          $excluded=0;
          $num_of_assess_counter=0;
          $grade=0;
          $left_over = 0;


            foreach($num_of_assessments as $num)
            {
                //for($i=0; $i <= $num)
                $number = $num->num_of_assign;
                //echo "Total number of $assessment->assess_name: $number <br><br>";

            }


            foreach ($get_grade as $g)
          {
            //dummy/decoit
            $grade = $g->grade;
            $total = $total + $grade;

            if($grade != null)
            {
            $counter = $counter + 1;
            }
            else
               {
                $counter;
                echo $counter;
               }

          $left_over = $number - $counter;
         // echo $left_over;


          if (!$counter <= 0){
          $average = round(($total/$counter), 3);
          }
          }
          $assess_name_output = "<u>$num->assess_name:</u> ";
          $average_output = "Current Average: $average%"."<br>";
          $number_output = "Total assessments: $number"."<br>";
          $left_over_output = "Remaining assessments: $left_over <br>";



          $average_adder = $average_adder + $average;
          //echo $average_adder."<br>";
          if ($average !=0){
          $average_counter = $average_counter + 1;
          //echo $average_counter."<br>";
          array_push($currentStanding, $assess_name_output,$average_output, $number_output, $left_over_output);
          }
          else{
              $average_counter = $average_counter;
          }

       $counter = 0;
       $average = 0;
       $total = 0;
       $left_over=0;
       }

       global $class_average;
       if(!$average_counter == 0){
       $class_average = round($average_adder/$average_counter, 2);
       }
       //$class_avg_output = "------------------------------Current class average: $class_average%-------------------------------";

       //assigning the average to the global variable

       $course_details = $course->getCourseDetails($course_id);
       $goal_grade;
       $num_of_assessments;

       foreach($course_details as $goal)
           {
                $goal_grade = $goal->desired_grade;

           }

      $minimum = $scale->getCourseScale($course_id);
      $min_grade;
      foreach($minimum as $min)
          {

          if($min->grade == $goal_grade)
            {
               $min_grade = $min->minimum;
            }
          }

       $num_of_groups = $assess->getSingleUserAssessments($course_id);
       //assessment groups counter
       $num_of_assess_counter=0;
       foreach($num_of_groups as $num){
           $num_of_assess_counter++ ;
       }



       //this is where all the data is shoved, later displayed in views
      // array_push($currentStanding, $class_avg_output);
       return $class_average;

      }

 /*-------------------------Required Grades-----------------------------------*/
      public function gradeExpect($course_id)
      {

       $course = new Student_Model_DbTable_Course();
       $assess = new Student_Model_DbTable_Assessment();
       $grades = new Student_Model_DbTable_Grade();
       $scale = new Student_Model_DbTable_Scale();

        $gradeExpect = array();
        $allAssess = $assess->getSingleUserAssessments($course_id);

       // print_r($allAssess);

        $total=0;
          $counter = 0;
          $grade=0;
          $inner_deficit=0;
          $inner_deficit_adder=0;
          $inner_benefit = 0;
          $inner_benefit_adder = 0;
          $needed_avg=0;
          $inner_adjustment=0;
          $benefit_count=0;
          $group_adjustment=0;
          $assess_name = 0;
          $barrier=0;
          $no_left_over = 0;
          $new_grade_output = null;
          $excess_count = 0;
          $excess_adder = 0;
          $main_assessments_num_counter=0;
          $victim = 0;
          $saveScores = array();
          $excess_group_name = 0;
          $excess_found = false;
          

          
          foreach($allAssess as $assessment)
          {

          $assess_id = $assessment->id;
          $assess_name = "<u>".$assessment->assess_name."</u>:";
          //array_push($gradeExpect, $assess_name);
          echo "<br>";
          $get_grade = $grades->getSingleUserGrades($assess_id);

          $minimum = $scale->getCourseScale($course_id);
          
          $desired_grade = $course->getCourseDetails($course_id);
          $goal_grade;
          $goal_grade_output;
          $min_grade = 0;
          foreach($desired_grade as $goal)
           {
                $goal_grade = $goal->desired_grade;
                
           }
          foreach($minimum as $min)
          {

          if($min->grade == $goal_grade)
            {
               $min_grade = $min->minimum;
            }
          }

          
          foreach($get_grade as $g)
          {
                $grade = $g->grade;
                $total += $grade;
                if($grade != null)
            {
            $counter = $counter + 1;
            }
            else
               {
                $counter;
                
               }               

                $max_weight = $g->max_grade_in_percent; 
                $needed_avg = ($min_grade/100)*$max_weight;
               // echo "Needed average: $needed_avg% <br>";
                $theGrade = $g->grade;
                //echo "But GOT: $theGrade";
                $inPercentage = $g->grade_in_percent;
               // echo "But GOT%: $inPercentage.";

                if($inPercentage <= $needed_avg)
                {
                    //if the grade achieved is lesser than desired average
                    $inner_deficit = $min_grade - $theGrade;
                    
                   // echo "Inner deficit: $inner_deficit...... <br>";

                    $inner_deficit_adder += $inner_deficit;
                   // echo "Inner Deficit total: $inner_deficit_adder......";

                }
               if($inPercentage > $needed_avg)
               {

                $inner_benefit = $theGrade - $min_grade;
               // echo "Inner benefit: $inner_benefit...... <br>";
                $inner_benefit_adder += $inner_benefit;
              //  echo "Inner benefit total: $inner_benefit_adder......";
               }

                

                $inner_adjustment = $inner_deficit_adder - $inner_benefit_adder;
                //$group_adjustment +=$inner_adjustment;
                if ($inner_adjustment <= 0){
                    //if benefit count equals #of assessments in that group, then in good position
                    $benefit_count++;
                }
                else{
                //echo "...adjustment... $inner_adjustment";
                }
                
                
                               
          }


         
               
          /*attempting to get the number of assessments left in that group*/
          $num_of_assessments = $assess->getAssessDetails($assess_id);
           foreach($num_of_assessments as $num)
            {
                //for($i=0; $i <= $num)
                $number = $num->num_of_assign;
             //   echo "-----total assessments  $number---------";
                //echo "Total number of $assessment->assess_name: $number <br><br>";

            }
            //  echo "--subtracter-- $counter";
              ///echo "_adjustment_ $group_adjustment ";
             //  echo "_adjustment_ $inner_adjustment....";
               $left_over = $number - $counter;
               if(!$left_over ==0){
               $shift = $inner_adjustment/$left_over;
               
             //  echo "....add this baby $shift...";

               $new_score = round($min_grade + $shift, 2);
               if(!$new_score <= 0){

               if(!$min_grade == null){
               $new_grade_output =  "<br>Minimum grade now required on each remaining assessments: <b>$new_score%</b> <br>";
               }//echo $new_grade_output;
               else{
                   
               }
               array_push($gradeExpect, $assess_name);
               array_push($gradeExpect, $new_grade_output);
               }
               }
               /*------------------adjusts the grades and checks for left over assessments------------*/

               
               $over_limit = array();
               $excess=0;
               
               
               if (!$left_over <= 0){

                   if ($new_score > 100){
                       $excess_group_name = $assessment->assess_name;
                      // echo $excess_group_name;
                       //$barrier= "Projected goal for this group exceeds 100%. Hence, one of the possible adjustments is provided at the bottom. <br>";
                       $excess_count++;
                       $excess = $new_score-100;
                       $excess_adder += $excess;
                       $new_score_assignment = 100;
                       $victim = $assess_name;

                      // array_push($over_limit, $assessment->assess_name."-".$new_score);
                       
                       array_push($gradeExpect, null);
                   }

                   else{
                       // echo $assess_name;
                      //  echo $new_score;



                       //foreach($saveScores as $ss){
                      //     echo $ss;
                      // }

                       //echo $assess_name;
                        //concatenates assessment group name and new score and pushes it to the array
                       array_push($saveScores, $assessment->assess_name, $new_score);
                       //echo "_______new score_______ $new_score";

                   }
               }
               else{

                   $no_left_over = "No assessments left in this group<br>";
                   array_push($gradeExpect, $no_left_over);
               }



               $over_limit_adjustment=0;
               $counter = 0;
               $inner_deficit =0;
               $inner_deficit_adder=0;
               $inner_benefit =0;
               $inner_benefit_adder=0;
               $inner_adjustment = 0;
               $group_adjustment = 0;


         
            /*COUNTS THE NUMBER OF ASSESSMENT GROUPS*/           
             $main_assessments_num_counter++;
      }
                if (!($main_assessments_num_counter-$excess_count) == 0){
               $over_limit_adjustment = $excess_adder/($main_assessments_num_counter-$excess_count);
                }
               //echo $excess_adder;
               // echo $excess_count;
              // echo $over_limit_adjustment;


               //print_r($saveScores);
              // echo "---mulla---".$saveScores[0];
              if (!$excess_count <= 0){
                   

              $red_flag = 0;
              $final_adjustment_remark = null;
              foreach($saveScores as $score){
                  // echo $score;
                  if(!$score >100){
                  if(!is_numeric($score)){
                      $scoreStr = "<br><u>".$score."</u>:<br>";
                      array_push($gradeExpect, $scoreStr);
                  }
                  if(is_numeric($score)){
                      
                       $score += $over_limit_adjustment;
                       if(!$score > 100){
                       $scoreNum = "<b>".$score."% </b>";
                       array_push($gradeExpect, $scoreNum);
                       }
                       else{
                           $red_flag++;
                       }
                       }
                    }
                    else{
                        $red_flag ++;
                    }
              }

              if($red_flag <= 0){

              $final_adjustment_remark = "<br><br>Since the score projection for <b>$excess_group_name</b> exceeded 100%, you can still get '<b>$goal_grade</b>' in the course by".
                  " scoring <b>$new_score_assignment%</b> on remaining assessments in this group and the following on the rest:<br> ";
                  array_push($gradeExpect, $final_adjustment_remark);
              }

              else{

                  $final_adjustment_remark = "<br><br> It is <u>not possible</u> to achieve a final grade of '<b>$goal_grade</b>' in this course with your current standing.".
                  " Please change your desired grade to the next less letter grade.";
                  array_push($gradeExpect, $final_adjustment_remark);
              }

               }       

        
        array_push($gradeExpect,null);

        
        return $gradeExpect;

      }



}